/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.util;

import com.alibaba.common.lang.StringUtil;
import com.pingan.mobilegw.core.model.ServiceConfig;
import com.pingan.mobilegw.core.model.enums.ControlKey;
import org.apache.commons.lang.StringUtils;

/**
 * 动态超时功能的Util类
 * 
 * @author michael.duw
 * @version $Id: DynamicTimeOutUtil.java, v 0.1 2014-3-17 上午10:51:51 michael.duw Exp $
 */
public class DynamicTimeOutUtil {

    /** 超时时间的默认分隔符 */
    private static final String DEFAULT_SEPATATOR_TIMEOUTS = ",";

    /**
     * 从serviceConfig解读出RPC超时配置
     * 
     * @param serviceConfig
     * @return
     */
    public static String[] getTimeOuts(ServiceConfig serviceConfig) {

        String timeOuts = serviceConfig.getControlRules().get(ControlKey.TIME_OUT);
        String[] times = null;

        if (StringUtils.isNotBlank(timeOuts)) {
            times = StringUtil.split(timeOuts, DEFAULT_SEPATATOR_TIMEOUTS);
        }

        return times;

    }

    /**
     * 从serviceConfig解读出RPC超时配置-如果设置多个，取第一个
     * 
     * @param serviceConfig
     * @return
     */
    public static String getTimeOut(ServiceConfig serviceConfig) {

        String timeOuts = serviceConfig.getControlRules().get(ControlKey.TIME_OUT);
        String[] times = null;

        if (StringUtils.isNotBlank(timeOuts)) {
            times = StringUtil.split(timeOuts, DEFAULT_SEPATATOR_TIMEOUTS);
            return times[0];
        }

        return null;

    }

    /**
     * 获取serviceKey
     * 
     * @param sysName 系统名
     * @param timeOut 超时时间
     * @return
     */
    public static String getServiceKey(String sysName, String timeOut) {

        return StringUtil.isBlank(timeOut) ? sysName : sysName + "_" + timeOut;

    }

}
